<template>
	<view>
		<view class="wrap">
			<image class="bg_img" :src=" '/images/bg.png' | formatImgUrl" mode=""></image>
			<view class="head">
				<view class="tit" v-if="userInfo.is_writer==1">
					当前总佣金 <image :src=" '/images/nosee.png' | formatImgUrl" v-if="!isShow" @tap.stop="changeEye">
					</image>
					<image :src=" '/images/see.png' | formatImgUrl" v-else @tap.stop="changeEye"></image>
					<view class="withdraw" @click="$go('/pages/finance/finance_dea')" v-if="datas == 1">
						<image :src=" '/images/withdraw_icon.png'| formatImgUrl" mode=""></image>
						提现中 <u-icon name="arrow-right" size="12" color="#fff" class="withdraw_icon"></u-icon>

					</view>
				</view>

				<view class="tit" v-else>
					当前总粮币 <image :src=" '/images/nosee.png' | formatImgUrl" v-if="!isShow" @tap.stop="changeEye">
						<image :src=" '/images/see.png' | formatImgUrl" v-else @tap.stop="changeEye"></image>
					<!-- <view class="withdraw" @click="$go('/pages/user/bill?type=1')" >
						<image :src=" '/images/withdraw_icon.png'| formatImgUrl" mode=""></image>
						提现中 <u-icon name="arrow-right" size="12" color="#fff" class="withdraw_icon"></u-icon>
					</view> -->
				</view>

				<view v-if="userInfo.is_writer==1" class="money_num">
					{{ isShow ? userInfo.with_month : '***'}}元
				</view>
				<view v-else>
					{{ isShow ? userInfo.wallet : '***'}}枚
				</view>


				<view class="consumption" v-if="userInfo.is_writer==1">
					<view class="today">
						<view class="total_tit">
							粮币余额(枚)
						</view>
						<text class="num">{{isShow ? Number(userInfo.wallet).toFixed(0) : '***'}}</text>
					</view>
					<view class="today">
						<view class="total_tit">
							累计收益(元)
						</view>
						<text class="num">{{isShow ? userInfo.commission : '***'}}</text>
					</view>
					<view class="today">
						<view class="total_tit">
							昨日收益(元)
						</view>
						<text class="num">{{isShow ? userInfo.z_money : '***'}}</text>
					</view>
				</view>

				<view class="consumption" v-else>
					<view class="today">
						<view class="total_tit">
							今日消费(元)
						</view>
						<text class="num">{{isShow ? userInfo.days_money : '***'}}</text>
					</view>
					<view class="today">
						<view class="total_tit">
							累计消费(元)
						</view>
						<text class="num">{{isShow ? userInfo.z_days_money : '***'}}</text>
					</view>
				</view>



			</view>
			<view class="choose" v-if="userInfo.is_writer == 1">
				<view class="item" style="z-index: 9999999;" v-for="(i,index) in chooseList2" :key="index"
					@click="$go(i.cl_url)">
					<image :src="i.url | formatImgUrl"></image>
					<text class="tit">{{i.text}}</text>
				</view>
			</view>
			
			<view class="choose" v-else>
				<view class="item" style="z-index: 9999999;" v-for="(i,index) in chooseList" :key="index"
					@click="$go(i.cl_url)">
					<image :src="i.url | formatImgUrl"></image>
					<text class="tit">{{i.text}}</text>
				</view>
			</view>

			<view class="discount">
				<view class="tit">
					<text class="title">优惠券</text>
					<view class="tip" @click="qiehuan()">
						<image :src="'/images/switch_icon.png' | formatImgUrl" class="icon"></image>
						{{navList[navIndex-1].name}}
					</view>
				</view>
				<view class="item" v-for="(item,index) in couponList">
					<view class="left">
						<view class="left_tit">
							<view class="name">
								{{item.coupon.title}}券
							</view>
							<view class="time">
								有效期至 <text>{{$options.filters.parseTime(item.validity,'{y}-{m}-{d}') }}</text>
							</view>
						</view>
						<view class="left_wrap">
							<view class="title_dic">
								<text class="num">{{item.coupon.price}}币</text>约稿红包
							</view>
							<view class="info_dic">
								{{item.coupon.des}}
							</view>
						</view>
					</view>
					<view class="right" @click="$go('/pages/user/coupon?id='+item.id)">
						待使用
					</view>
				</view>

				<view class="empytView" v-if="couponList.length<=0">
					<image :src="'/images/kong.png' | formatImgUrl" mode="widthFix" style="width: 200rpx;" />
				</view>
			</view>
		</view>
		
		<dLoading :status="loading"></dLoading>
	</view>
</template>

<script>
	import * as db from '@/common/db.js'
	export default {
		data() {
			return {
				userInfo: this.$db.get('userInfo'),
				navIndex: 1,
				xian: false,
				couponList: [],
				navList: [{
					id: 1,
					name: '可使用'
				}, {
					id: 2,
					name: '待生效'
				}, {
					id: 3,
					name: '已使用'
				}, {
					id: 4,
					name: '已生效'
				}],
				chooseList: [{
						url: '/images/property_icon1.png',
						text: '粮币充值',
						id: 1,
						cl_url: '/pages/user/coin?type=1'
					},
					{
						url: '/images/property_icon5.png',
						text: '粮币兑换',
						id: 2,
						cl_url: '/pages/user/coin?type=2'
					},
					{
						url: '/images/property_icon3.png',
						text: '账单记录',
						id: 3,
						cl_url: '/pages/user/bill'
					}
				],
				chooseList2: [{
						url: '/images/property_icon4.png',
						text: '收益提现',
						id: 0,
						cl_url: '/pages/user/withdrawal'
					},
					{
						url: '/images/property_icon1.png',
						text: '粮币充值',
						id: 1,
						cl_url: '/pages/user/coin?type=1'
					},
					{
						url: '/images/property_icon5.png',
						text: '兑换粮币',
						id: 2,
						cl_url: '/pages/user/coin?type=2'
					},
					{
						url: '/images/property_icon3.png',
						text: '账单记录',
						id: 3,
						cl_url: '/pages/user/bill'
					}],
				isShow: true,
				datas: 0,
				loading: true
			};
		},
		onLoad() {
			let auth = db.get('auth');
			console.log(auth, 777777777)
			//用户存在，不跳转，不存在直接跳转
			if (!auth) {
				// toLogin()
				uni.navigateTo({

					url: '/pages/login/login'

				})
				return;
			}
		},
		onShow() {
			this.getUserInfo()
			this.getCouponList()
		},
		methods: {
			changeEye() {
				console.log('changeEye',111);
				this.isShow = !this.isShow
				this.$forceUpdate()
			},
			tiaozhaun(i) {
				console.log(i, 999999999)
			},
			qiehuan() {

				if (this.navIndex > 3) {
					this.navIndex = 1;
				} else {
					this.navIndex++
				}

				this.getCouponList()
			},
			go_tixian() {
				if (this.userInfo.is_auth == 1) {
					uni.navigateTo({
						url: '/pages/user/withdrawal'
					})
				} else {

					this.$common.errorToShow("请先实名认证")


					setTimeout(function() {
						uni.navigateTo({
							url: '/pages/user/auth'
						})

					}, 500);

				}

			},
			getUserInfo() {
				this.$api.default.request('user/userInfo', {}, 'POST', false).then((res) => {
					if (res.code) {
						this.userInfo = res.user

						if (this.userInfo.is_writer == 1) {
							this.chooseList = [{
									url: '/images/property_icon4.png',
									text: '收益提现',
									id: 0,
									cl_url: '/pages/user/withdrawal'
								},
								{
									url: '/images/property_icon1.png',
									text: '粮币充值',
									id: 1,
									cl_url: '/pages/user/coin?type=1'
								},
								{
									url: '/images/property_icon5.png',
									text: '兑换粮币',
									id: 2,
									cl_url: '/pages/user/coin?type=2'
								},
								{
									url: '/images/property_icon3.png',
									text: '账单记录',
									id: 3,
									cl_url: '/pages/user/bill'
								}]

						}
					}
					
					this.loading = false
				})
				
				this.$api.default.request('user/getMyTxChe', {}, 'POST', false).then((res) => {
					if (res.code) {
						this.datas = res.data
					}
				})
			},
			getCouponList() {
				this.$api.default.request('user/getCouponList', {
					type: this.navIndex
				}).then((res) => {
					if (res.code) {
						this.couponList = res.data
					}
				})
			},
			// 点击切换
			navTap(item) {
				this.navIndex = item.id
				this.getCouponList()
			},

			xianshow() {
				this.xian = !this.xian
			}
		}
	}
</script>

<style lang="scss">
	.wrap {
		width: 100%;
		height: 100vh;
		background: linear-gradient(180deg, #45C4B0 17%, #F2F5F2 57%);
		background-size: 100% 100%;
		position: relative;

		.bg_img {
			// transform: rotate(10.5deg);
			position: absolute;
			right: 0;
			top: 8rpx;
			z-index: 0;
		}

		.head {
			padding: 26rpx 30rpx;
			font-size: 32rpx;
			color: #fff;
			font-weight: 300;

			.tit {
				display: flex;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
					object-fit: cover;
					margin-left: 8rpx;
				}

				.withdraw {
					width: 180rpx;
					height: 42rpx;
					border-top-left-radius: 25rpx;
					border-bottom-left-radius: 25rpx;
					background: rgba(255, 255, 255, 0.2);
					font-size: 24rpx;
					color: #FFFFFF;
					display: flex;
					align-items: center;
					padding-bottom: 2rpx;
					position: absolute;
					right: 0;

					image {
						width: 26rpx;
						height: 26rpx;
						object-fit: cover;
						margin-left: 20rpx;
						margin-right: 4rpx;
					}
				}
			}

			.money_num {
				font-size: 60rpx;
				font-weight: bold;
				color: #FFFFFF;
			}

			.consumption {
				display: flex;
				margin-top: 40rpx;

				.today {
					margin-right: 80rpx;

					.total_tit {
						font-size: 24rpx;
						font-weight: 300;
						color: #FFFFFF;
					}

					.num {
						font-size: 28rpx;
						color: #FFFFFF;
						font-weight: bold;
					}

				}
			}
		}

		.choose {
			margin: 40rpx 30rpx;
			height: 166rpx;
			border-radius: 24rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;

				image {
					width: 76rpx;
					height: 76rpx;
					object-fit: cover;
					margin-bottom: 4rpx;
					// border: 1rpx dotted #eee;
				}

				.tit {
					font-size: 24rpx;
					color: #3D3D3D;
				}
			}
		}

		.discount {
			margin: 0 30rpx 0;
			background: #fff;
			box-sizing: border-box;
			padding: 40rpx 30rpx;
			border-radius: 24rpx;

			.tit {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.title {
					font-size: 32rpx;
					font-weight: bold;
					color: #3D3D3D;
				}

				.tip {
					font-size: 24rpx;
					color: #999999;
					display: flex;
					align-items: center;

					.icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 8rpx;
						// border: 1rpx dotted #eee;
						object-fit: cover;
					}
				}
			}

			.item {
				width: 100%;
				height: 238rpx;
				background-color: #D8D8D8;
				position: relative;
				border-radius: 8rpx;
				overflow: hidden;
				margin-top: 20rpx;
				box-shadow: 0 3rpx 10rpx -1rpx #4A4A4A;

				.left {
					width: 568rpx;
					height: 238rpx;
					background: url('https://cdnauth.dreamyuewen.com/images/dic_bg.png') no-repeat;
					background-size: cover;
					position: absolute;
					top: 0;
					left: 0;
					z-index: 2;

					.left_tit {
						padding: 0 14rpx;
						height: 68rpx;
						display: flex;
						margin: 0 auto;
						justify-content: space-between;
						align-items: center;
						border-bottom: 1px dashed #d8d8d8;

						.name {
							font-size: 28rpx;
							font-weight: 500;
							color: #3D3D3D;
							position: relative;
							margin-left: 12rpx;

							&::before {
								content: '';
								width: 4rpx;
								height: 32rpx;
								border-radius: 96rpx;
								background: #45C4B0;
								position: absolute;
								left: -14rpx;
								top: 5rpx;
							}
						}

						.time {
							font-size: 20rpx;
							color: #999999;
						}
					}

					.left_wrap {
						padding: 20rpx;

						.title_dic {
							font-size: 48rpx;
							font-weight: 500;
							color: #3D3D3D;

							.num {
								color: #45C4B0
							}
						}

						.info_dic {
							font-size: 28rpx;
							font-weight: 300;
							color: #999999;
							overflow: hidden; //超出的文本隐藏
							text-overflow: ellipsis; //溢出用省略号显示
							white-space: nowrap; //溢出不换行
						}
					}
				}

				.right {
					width: 74rpx;
					height: 100%;
					background-color: #45C4B0;
					position: absolute;
					top: 0;
					right: 0;
					z-index: 1;
					font-size: 28rpx;
					font-weight: bold;
					text-align: center;
					color: #FFFFFF;
					letter-spacing: 8rpx;
					writing-mode: vertical-rl;
					padding-right: 10rpx;
				}
			}
		}
	}
</style>